package service

import (
	"context"
	"errors"
	"video-srv/basic/config"
	__ "video-srv/basic/proto"
	"video-srv/handler/models"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedVideoServer
}

// 获取用户信息
func (s *Server) GetUserInfo(_ context.Context, in *__.GetUserInfoReq) (*__.GetUserInfoResp, error) {
	var user models.User
	user.Username = in.Username
	user.Password = in.Password
	err := user.GetUserInfo(in.Username)
	if err != nil {
		return nil, errors.New("获取用户信息失败")
	}

	return &__.GetUserInfoResp{
		UserId:   int64(user.ID),
		Username: user.Username,
		Password: user.Password,
	}, nil
}

// 创建用户
func (s *Server) CreateUser(_ context.Context, in *__.CreateUserReq) (*__.CreateUserResp, error) {
	var user models.User
	user.Username = in.Username
	user.Password = in.Password
	err := user.GetUserInfo(in.Username)
	if err != nil {
		return nil, errors.New("获取用户信息失败")
	}
	if user.ID != 0 {
		return nil, errors.New("用户已存在")
	}

	err = user.CreateUser()
	if err != nil {
		return nil, errors.New("创建用户失败")
	}

	return &__.CreateUserResp{
		UserId: int64(user.ID),
	}, nil
}

// 发布视频
func (s *Server) AddVideo(_ context.Context, in *__.AddVideoReq) (*__.AddVideoResp, error) {
	var err error
	db := config.DB

	//判断用户是否存在
	var user models.User
	user.ID = uint(in.UserId)
	err = user.GetUserById(db, in.UserId)
	if err != nil {
		return nil, errors.New("获取用户信息失败")
	}
	if user.ID == 0 {
		return nil, errors.New("用户不存在")
	}

	//开启事务
	tx := db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err = tx.Error; err != nil {
		tx.Rollback()
		return nil, err
	}

	//发布视频
	var video models.Video
	video = models.Video{
		UserId:   int(in.UserId),
		Title:    in.Title,
		Desc:     in.Desc,
		CoverUrl: in.CoverUrl,
	}

	if err = video.AddVideo(tx); err != nil {
		tx.Rollback()
		return nil, errors.New("视频发布失败")
	}

	//更新用户视频数量
	if err = user.UpdateVideoCount(tx, in.UserId); err != nil {
		tx.Rollback()
		return nil, errors.New("更新用户视频数量失败")
	}

	//提交事务
	if err = tx.Commit().Error; err != nil {
		tx.Rollback()
		return nil, err
	}

	return &__.AddVideoResp{
		VideoId: int64(video.ID),
	}, nil
}

// 视频列表
func (s *Server) GetVideoList(_ context.Context, in *__.GetVideoListReq) (*__.GetVideoListResp, error) {
	var err error

	var video models.Video
	list, err := video.GetVideoList(int(in.Page), int(in.PageSize), in.TypeId)
	if err != nil {
		return nil, errors.New("视频列表展示失败")
	}

	var videoList []*__.GetVideoInfo
	for _, v := range list {
		videoList = append(videoList, &__.GetVideoInfo{
			VideoId:   int64(v.ID),
			UserId:    int64(v.UserId),
			Title:     v.Title,
			CoverUrl:  v.CoverUrl,
			TypeId:    int64(v.TypeId),
			LookCount: int64(v.LookCount),
			LikeCount: int64(v.LikeCount),
		})
	}

	return &__.GetVideoListResp{
		VideoList: videoList,
	}, nil
}

// 视频详情
func (s *Server) GetVideoDetail(_ context.Context, in *__.GetVideoDetailReq) (*__.GetVideoDetailResp, error) {
	var err error
	db := config.DB

	var video models.Video

	if err = video.GetVideoById(db, in.VideoId); err != nil {
		return nil, err
	}

	if video.ID == 0 {
		return nil, errors.New("视频不存在")
	}

	if err = video.GetVideoDetail(db, in.VideoId); err != nil {
		return nil, errors.New("获取详情失败")
	}

	//更新视频观看次数
	if err = video.UpdateVideoLookCount(db, in.VideoId); err != nil {
		return nil, errors.New("更新视频观看次数失败")
	}

	var check models.Checks
	if err = check.GetVideoCheck(db, in.VideoId); err != nil {
		return nil, err
	}

	return &__.GetVideoDetailResp{
		VideoId:     int64(video.ID),
		UserId:      int64(video.UserId),
		Title:       video.Title,
		CoverUrl:    video.CoverUrl,
		LookCount:   int64(video.LookCount),
		LikeCount:   int64(video.LikeCount),
		CheckStatus: int64(check.Status),
	}, nil
}

// 点赞
func (s *Server) AddLike(_ context.Context, in *__.AddLikeReq) (*__.AddLikeResp, error) {
	var err error
	db := config.DB

	//判断用户是否存在
	var user models.User
	user.ID = uint(in.UserId)
	if err = user.GetUserById(db, in.UserId); err != nil {
		return nil, errors.New("用户获取失败")
	}
	if user.ID == 0 {
		return nil, errors.New("用户不存在")
	}

	//判断视频是否存在
	var video models.Video
	video.ID = uint(in.VideoId)
	if err = video.GetVideoById(db, in.VideoId); err != nil {
		return nil, errors.New("视频获取失败")
	}
	if video.ID == 0 {
		return nil, errors.New("视频不存在")
	}

	//判断点赞
	var like models.Like
	if err = like.GetLikeStatus(db, in.UserId, in.VideoId); err != nil {
		return nil, errors.New("视频点赞获取失败")
	}
	if like.ID != 0 {
		return nil, errors.New("该视频已点赞")
	}

	//点赞
	like.UserId = int(in.UserId)
	like.VideoId = int(in.VideoId)
	if err = like.AddLike(db); err != nil {
		return nil, errors.New("点赞失败")
	}

	//更新用户点赞量
	if err = user.UpdateUserLikeCount(db, in.UserId); err != nil {
		return nil, errors.New("同步用户点赞失败")
	}

	//更新视频点赞
	if err = video.UpdateVideoLikeCount(db, in.VideoId); err != nil {
		return nil, errors.New("同步视频点赞失败")
	}

	return &__.AddLikeResp{
		IsLike: true,
	}, nil
}

// 取消点赞
func (s *Server) DelLike(_ context.Context, in *__.DelLikeReq) (*__.DelLikeResp, error) {
	var err error
	db := config.DB

	//判断用户是否存在
	var user models.User
	if err = user.GetUserById(db, in.UserId); err != nil {
		return nil, errors.New("用户获取失败")
	}
	if user.ID == 0 {
		return nil, errors.New("用户不存在")
	}

	//判断视频是否存在
	var video models.Video
	if err = video.GetVideoById(db, in.VideoId); err != nil {
		return nil, errors.New("视频获取失败")
	}
	if video.ID == 0 {
		return nil, errors.New("视频不存在")
	}

	//判断点赞
	var like models.Like
	if err = like.GetLikeStatus(db, in.UserId, in.VideoId); err != nil {
		return nil, errors.New("视频点赞获取失败")
	}
	if like.ID == 0 {
		return nil, errors.New("该视频未点赞")
	}

	//取消点赞
	like.UserId = int(in.UserId)
	like.VideoId = int(in.VideoId)
	if err = like.DelLike(db); err != nil {
		return nil, errors.New("取消点赞失败")
	}

	//同步用户点赞量
	if err = user.DelUserLikeCount(db, in.UserId); err != nil {
		return nil, errors.New("同步用户点赞失败")
	}

	//同步视频点赞
	if err = video.DelVideoLikeCount(db, in.VideoId); err != nil {
		return nil, errors.New("同步视频点赞失败")
	}

	return &__.DelLikeResp{
		IsLike: false,
	}, nil
}

// 查询用户点赞量
func (s *Server) GetUserLikeCount(_ context.Context, in *__.GetUserLikeCountReq) (*__.GetUserLikeCountResp, error) {
	var err error

	var user models.User
	user.ID = uint(in.UserId)
	if err = user.GetUserLikeCount(config.DB, in.UserId); err != nil {
		return nil, err
	}

	if user.ID == 0 {
		return nil, errors.New("用户点赞量查询失败")
	}

	return &__.GetUserLikeCountResp{
		LikeCount: int64(user.LikeCount),
	}, nil
}

// 查询视频点赞量
func (s *Server) GetVideoLikeCount(_ context.Context, in *__.GetVideoLikeCountReq) (*__.GetVideoLikeCountResp, error) {
	var err error

	var video models.Video
	video.ID = uint(in.VideoId)
	if err = video.GetVideoLikeCount(config.DB, in.VideoId); err != nil {
		return nil, err
	}

	if video.ID == 0 {
		return nil, errors.New("视频点赞量查询失败")
	}

	return &__.GetVideoLikeCountResp{
		LikeCount: int64(video.LikeCount),
	}, nil
}
